package org.wesc.boot.webapi.api.system;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.wesc.boot.common.annotation.ApiEndpoint;
import org.wesc.boot.common.api.R;
import org.wesc.boot.dao.domain.QueryRequest;
import org.wesc.boot.dao.entity.Dict;
import org.wesc.boot.service.system.dict.DictAdvanceService;
import org.wesc.boot.webapi.base.BaseApi;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author Wesley
 */
@Api(tags={"字典接口"})
@Slf4j
@Validated
@RestController
@RequestMapping("/api/dict")
public class DictApi extends BaseApi {

    private final DictAdvanceService dictAdvanceService;

    @Autowired
    public DictApi(DictAdvanceService dictAdvanceService) {
        this.dictAdvanceService = dictAdvanceService;
    }

    @ApiOperation(value = "获取字典列表")
    @GetMapping
    @RequiresPermissions("dict:view")
    public R<Map<String, Object>> dictList(QueryRequest request, Dict dict) {
        return R.data(getDataTable(this.dictAdvanceService.pageQueryDictList(request, dict)));
    }

    @ApiOperation(value = "新增字典")
    @PostMapping
    @RequiresPermissions("dict:add")
    @ApiEndpoint(operation = "新增字典", exception = "新增字典失败")
    public R<String> addDict(@Valid Dict dict) {
        try {
            this.dictAdvanceService.createDict(dict);
            return R.success();
        } catch (Exception e) {
            String message = "新增字典成功";
            log.error(message, e);
            return R.fail(message);
        }
    }

    @ApiOperation(value = "删除字典")
    @DeleteMapping("/{dictIds}")
    @RequiresPermissions("dict:delete")
    @ApiEndpoint(operation = "删除字典", exception = "删除字典失败")
    public R<String> deleteDict(@NotBlank(message = "{required}") @PathVariable String dictIds) {
        try {
            String[] ids = dictIds.split(StringPool.COMMA);
            this.dictAdvanceService.batchDeleteDict(ids);
            return R.success();
        } catch (Exception e) {
            String message = "删除字典失败";
            log.error(message, e);
            return R.fail(message);
        }
    }

    @ApiOperation(value = "修改字典")
    @PutMapping
    @RequiresPermissions("dict:update")
    @ApiEndpoint(operation = "修改字典", exception = "修改字典失败")
    public R<String> updateDict(Dict dict) {
        try {
            this.dictAdvanceService.updateDict(dict);
            return R.success();
        } catch (Exception e) {
            String message = "修改字典失败";
            log.error(message, e);
            return R.fail(message);
        }
    }

    @ApiOperation(value = "导出字典")
    @PostMapping("excel/export")
    @RequiresPermissions("dict:export")
    @ApiEndpoint(operation = "导出字典", exception = "导出字典失败")
    public void exportExcel(QueryRequest request, Dict dict, HttpServletResponse response) throws IOException {
        response.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");
        String sheetName = "字典列表";
        List<Dict> dictList = this.dictAdvanceService.pageQueryDictList(request, dict).getRecords();
        EasyExcel.write(response.getOutputStream(), Dict.class).sheet(sheetName).doWrite(dictList);
    }

}
